<?php

require_once 'cms/core/om/om/BaseBinaryFile.php';


/**
 * Skeleton subclass for representing a row from the 'binary_file' table.
 *
 * 
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package core.om
 */	
class BinaryFile extends BaseBinaryFile {

    /**
     * Return a more readable filesize i.e. 4KB
     */
    public function getHumanReadableFileSize() {

        $KBsize = 1024;
        $MBsize = $KBsize * 1000;
        $GBsize = $MBsize * 1000;
        if ((int)$this->getFileSize() >= $GBsize) {
            return (round($this->getFileSize() / $GBsize, 1))."Gb";
        } else if ((int)$this->getFileSize() >= $MBsize) {
            return (round($this->getFileSize() / $MBsize, 1))."Mb";
        } else if ((int)$this->getFileSize() >= $KBsize) {
            return (round($this->getFileSize() / $KBsize, 1))."Kb";
        }
        return $this->getFileSize()."bytes";

    }


    /**
     * Save a BinaryFile posted by a form.
     *
     * @param formFile $_FILES entry from a form.
     */
    public function saveForm(&$formFile, $conn=null) {

        $log =& LoggerManager::getLogger("BinaryFile");

        if($log->isDebugEnabled())
            $log->debug("Saving BinaryFile using form");

        $this->setFileName($formFile["name"]);
        $this->setFileSize($formFile["size"]);
        $this->setContentType($formFile["type"]);
        $this->setModifiedDate(date(_Core::FORMAT_SQLDATETIME));
        $this->setFileData(fread(fopen($formFile["tmp_name"], "rb"), $formFile["size"]));

        if($this->getBinaryFileId() > 0) {

            if($log->isDebugEnabled())
                $log->debug("Trying to update.");

            BinaryFilePeer::doUpdate($this, $conn);

        } else {

            if($log->isDebugEnabled())
                $log->debug("Trying to insert.");

            $this->setInsertDate(date(_Core::FORMAT_SQLDATETIME));
            $this->setBinaryFileId(
                BinaryFilePeer::doInsert($this, $conn)
            );

        }

        return true;
    }


    /**
     * Save a BinaryFile using the filesystem.
     *
     * @param filename The full path to the filename.
     * @param conn The database connection to use.
     */
    public function saveFile($realfile, $filename=null, $conn=null) {

        $log =& LoggerManager::getLogger("BinaryFile");

        if($log->isDebugEnabled())
            $log->debug("Saving BinaryFile using filesystem");

        if(!file_exists($realfile))
            throw new Exception("File ${filename} could not found.");

        $this->setFileName(is_null($filename) ? basename($realfile) : $filename);
        $this->setFileSize(filesize($realfile));
        $this->setModifiedDate(date(_Core::FORMAT_SQLDATETIME));
        $this->setFileData(file_get_contents($realfile));

        if($this->getBinaryFileId() > 0) {

            if($log->isDebugEnabled())
                $log->debug("Trying to update.");

            BinaryFilePeer::doUpdate($this, $conn);

        } else {

            if($log->isDebugEnabled())
                $log->debug("Trying to insert.");

            $this->setInsertDate(date(_Core::FORMAT_SQLDATETIME));
            $this->setBinaryFileId(
                BinaryFilePeer::doInsert($this, $conn)
            );

        }

        return true;
    }

} // BinaryFile
